//
//  WBImageView.m
//  WBImageView
//
//  Created by mac on 16/1/9.
//  Copyright © 2016年 YF_S. All rights reserved.
//

#import "WBImageView.h"
#import "UIImageView+WebCache.h"
#import "WBScrollViewController.h"
#import "WBImageViewItem.h"

#define kWidth self.frame.size.width
#define kSpace 10

@implementation WBImageView

//setData
-(void)setData:(NSArray *)data{

    _data = data;
    
    //
    for (UIView *view in self.subviews) {
        
        [view removeFromSuperview];
    }
    
    //1.根据数据计算高度
    CGFloat height = [self heightForCount:_data.count];
    
    self.height = height;
    
    //初始化数组
    _items = [NSMutableArray array];
    
    //2.创建控件
    for (int i = 0; i < _data.count; i++) {
        
        WBImageViewItem *item = [[WBImageViewItem alloc]initWithFrame:[self frameForItemIndex:i]];
        
        [item sd_setImageWithURL:[NSURL URLWithString:_data[i]]];
        
//        item.contentMode = UIViewContentModeScaleAspectFit;
        
        //记录当前的frame属性
        item.originFrame = item.frame;
        
        //记录当前的下标
        item.index = i;
        
        //Item的触摸响应
        item.userInteractionEnabled = YES;
        
        //给Item添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAct:)];
        
        [item addGestureRecognizer:tap];
        
        //将每个视图添加到数组中
        [_items addObject:item];
        
        [self addSubview:item];
    }
}

//手势的点击响应事件
-(void)tapAct:(UITapGestureRecognizer *)tap{

    //获取到被点击的控件
    WBImageViewItem *item = (WBImageViewItem *)tap.view;
    
    //将单元格拿到最顶上来
    [self bringCellToFront];
    
    //创建模态视图
    WBScrollViewController *wbSVC = [[WBScrollViewController alloc]init];
    
    //将被点击的控件传递给wbSVC
    wbSVC.item = item;
    
    wbSVC.wbImageView = self;
    
    wbSVC.data = self.data;
    
    //弹出模态视图
    [self.viewController presentViewController:wbSVC animated:YES completion:nil];
//    [self bringSubviewToFront:<#(nonnull UIView *)#>];

}

//通过响应者链 -- >返回单元格
-(void)bringCellToFront{

    //获取到下一响应者对象
    UIResponder *nextResponder = self.nextResponder;
    
    while (nextResponder) {
        
        if ([nextResponder isKindOfClass:[UITableViewCell class]]) {
            
            //将当前的cell单元格放到最顶上来
            [((UITableViewCell *)nextResponder).superview bringSubviewToFront:(UITableViewCell *)nextResponder];
            
            return;
        }
        
        //若果不是 则继续获取到下一响应者对象
        nextResponder = nextResponder.nextResponder;
    }
}

//根据数据计算高度
-(CGFloat)heightForCount:(NSInteger)count{

    //计算行数
    long row = count/3;
    
    if (count%3 != 0) {
        
        row++;
    }
    
    //每个图片的宽度
    CGFloat itemW = (kWidth - 4*kSpace)/3;
    
    CGFloat height = itemW *row +kSpace*(row+1);
    
    return height;
}

//计算每个imgV的frame
-(CGRect)frameForItemIndex:(int)count{

    //每个图片的宽度
    CGFloat itemW = (kWidth - 4*kSpace)/3;
    
    //计算xy轴的坐标
    CGFloat x = count%3*itemW +kSpace *(count%3+1);
    
    CGFloat y = count/3*itemW +kSpace *(count/3+1);
    
    return CGRectMake(x, y, itemW, itemW);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
